package com.codeman.java8;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
 * Lambda表达式测试题
 */
public class LambdaTest {
    public static void main(String[] args) {
        List<Transaction> transactions = loadData();
        // 1. 找出2011年发生的所有交易，并按交易额排序
//        transactions.stream()
//        .filter(transaction -> transaction.getYear() == 2011)
//        .sorted(Comparator.comparingInt(Transaction::getValue).reversed())
//        .collect(Collectors.toList())
//        .forEach(System.out::println);
        // Comparator.comparingInt(Transaction::getValue).reversed() 按照value降序排序

        // 2. 交易员在哪些不同的城市工作过
//        transactions.stream()
//        .map(transaction -> transaction.getTrader().getCity())
//        .distinct()
//        .collect(Collectors.toList())
//        .forEach(System.out::println);

        // 3. 查找所有来自Cambridge的交易员，并按姓名排序
//        transactions.stream()
//        .filter(transaction -> transaction.getTrader().getCity().equals("Cambridge"))
////                .peek(transaction -> System.out.println(transaction))
//        .sorted(Comparator.comparing(o -> o.getTrader().getName()))
//        .collect(Collectors.toList()).forEach(System.out::println);
        // .sorted(Comparator.comparing(o -> o.getTrader().getName()))

        // 4. 返回所有交易员的姓名字符串，并按字母顺序排序
//        transactions.stream()
//                .map(transaction -> transaction.getTrader().getName())
//                .distinct()
//                .sorted(String::compareTo)
//                .forEach(System.out::println);

        // 5. 有没有交易员在Milan工作的？
//        boolean milan = transactions.stream()
//                .anyMatch(transaction -> transaction.getTrader().getCity().equals("Milan2"));
//        System.out.println(milan);

        // 6. 打印生活在Cambridge的交易员的所有交易额
//        Integer sum = transactions.stream()
//                .filter(transaction -> transaction.getTrader().getCity().equals("Cambridge"))
//                .map(transaction -> transaction.getValue())
//                .reduce(0, Integer::sum);
////                .reduce(0, (a,b)->a+b);
//        System.out.println(sum);

        // 7. 所有交易中，最高的交易额为？
//        Optional<Integer> max = transactions.stream()
//                .map(transaction -> transaction.getValue())
//                .max(Integer::compareTo);
//        System.out.println(max.orElse(-1));

        // 8. 找到交易额最小的交易
        Optional<Integer> max = transactions.stream()
                .map(transaction -> transaction.getValue())
                .min(Integer::compareTo);
        System.out.println(max.orElse(-1));

    }

    private static List<Transaction> loadData() {
        Trader raoul = new Trader("Raoul", "Cambridge");
        Trader mario = new Trader("Mario", "Milan");
        Trader alan = new Trader("Alan", "Cambridge");
        Trader brian = new Trader("Brian", "Cambridge");

         return Arrays.asList(
                new Transaction(brian, 2011, 300),
                new Transaction(raoul, 2012, 1000),
                new Transaction(raoul, 2011, 400),
                new Transaction(mario, 2012, 710),
                new Transaction(mario, 2012, 700),
                new Transaction(alan, 2012, 950)
        );
    }
}

